/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "rep_logs_blinks.h"
#include "rep_logs_pages.h"
#include "rep_general.h"
#include "GR_write_html.h"

void
write_body_top_blinks_html_file(GraphFrame *gf,FILE *fp,int nbl,WebStatPtr msn);

int 
report_top_broken_links(GraphFrame *gf, char *name_file,int nbl,WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Top Broken Links ");
  write_start_body_html_file(fp,"#ffffff");
  write_body_top_blinks_html_file(gf,fp,nbl,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_top_blinks_html_file2(GraphFrame *gf,FILE *fp,int nbl,
				WebStatPtr msn)
{
  LNode_type *lurl_ext_ref = LOGR_get_list_url_external_referers(msn);
  LNode_type *independent_links = LOGR_get_list_independent_links(msn);
  LNode_type *nodes = get_list_vertices(gf);
  LNode_type *ptr = NULL;

  PEDGE *varray = NULL;
  WebIndependentLinkPtr *varray2 = NULL;
  WebIndependentPagePtr *varray3 = NULL;

  int i=0;
  char ntitle[100];
  int tn=0;
  int tlinks = 0;
  int tnlinks = 0;
  int tnurlbextref = 0;
  double tsize = LOGR_get_total_site_size(msn);
  long tacc = LOGR_get_total_site_naccesses(msn);
  time_t now;

  for(ptr = nodes->back; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      LNode_type *peo, *ledges_out = get_edges_out(v);
      for(peo = ledges_out->back ; peo != ledges_out ; peo = peo->back)
	{
	  PEDGE e = (PEDGE)peo->info;
	  PNODE u = get_to_vertex(e);
	  char *we = get_weight_edge(e);
	  int cedge = we ? atoi(we) : 0;
	  const char *att = u ? get_attribute(u,"code") : NULL;
	  int code = att ? atoi(att) : -1;

	  if(cedge > 0 && code >= 400)
	      tlinks++;
	}
    }

  varray = tlinks ? (PEDGE *)calloc(tlinks,sizeof(PEDGE)) : NULL;

  if(!varray && tlinks > 0) {
      fprintf(stderr,"Out of Memory write_body_top_blinks_html_file\n");
      abort();
  }

  if(tlinks) {
  i = 0;
  for(ptr = nodes->back; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      LNode_type *peo, *ledges_out = get_edges_out(v);
      for(peo = ledges_out->back ; peo != ledges_out ; peo = peo->back)
	{
	  PEDGE e = (PEDGE)peo->info;
	  PNODE u = get_to_vertex(e);
	  char *we = get_weight_edge(e);
	  int cedge = we ? atoi(we) : 0;
	  const char *att = u ? get_attribute(u,"code") : NULL;
	  int code = att ? atoi(att) : -1;

	  if(cedge > 0 && code >= 400)
	      varray[i++] = e;
	}
    }
  }

  for(ptr = independent_links->back; ptr != independent_links; ptr=ptr->back)
  {
    WebIndependentLinkPtr il = (WebIndependentLinkPtr)ptr->info;
    int http_code = LOGR_get_link_target_http_code(il);
    if(http_code >= 400)
	tnlinks++;
  }
  varray2 = tnlinks ? (WebIndependentLinkPtr*)calloc(tnlinks,
				        sizeof(WebIndependentLinkPtr)) : NULL;

  if(!varray2 && tnlinks > 0) {
      fprintf(stderr,"Out of Memory write_body_top_blinks_html_file\n");
      abort();
  }

  if(tnlinks > 0) {
   i = 0;
  for(ptr = independent_links->back; ptr != independent_links; ptr=ptr->back)
  {
    WebIndependentLinkPtr il = (WebIndependentLinkPtr)ptr->info;
    int http_code = LOGR_get_link_target_http_code(il);
    if(http_code >= 400)
	varray2[i++] = il;
  }
  }

  i=0;
  for(ptr = lurl_ext_ref->back; ptr != lurl_ext_ref; ptr = ptr->back)
  {
    WebIndependentPagePtr murle = (WebIndependentPagePtr)ptr->info;
    if(LOGR_get_ip_http_code(murle) >= 400)
	i++;
  }
  tnurlbextref = i;

  varray3 = tnurlbextref ? (WebIndependentPagePtr *)calloc(tnurlbextref,sizeof(WebIndependentPagePtr)) : NULL;

  if(!varray3 && tnurlbextref > 0) {
      fprintf(stderr,"Out of Memory write_body_top_ref_html_file\n");
      abort();
  }

  if(tnurlbextref) {
  i=0;
  for(ptr = lurl_ext_ref->back; ptr != lurl_ext_ref; ptr = ptr->back)
  {
    WebIndependentPagePtr murle = (WebIndependentPagePtr)ptr->info;
    if(LOGR_get_ip_http_code(murle) >= 400)
	varray3[i++] = murle;
  }
  }


  now = time(NULL);
  write_header_html_file(fp,1,"List of Top Broken Links");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");  
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Unique Broken Links : ");
  fprintf(fp, " %d URLs\n",tlinks);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Unique Independent Broken Links : ");
  fprintf(fp, " %d URLs\n",tnlinks);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Unique External Broken Links : ");
  fprintf(fp, " %d URLs\n",tnurlbextref);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Access Count : ");
  fprintf(fp, " %ld \n",tacc);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Size : ");
  fprintf(fp, " %.2f K Bytes\n",(float)(tsize/1024.0));
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RMVBLString);
  write_horizontal_rule_html_file(fp);

  if(tlinks > 0){
  if(nbl > tlinks)
      tn = tlinks;
  else
      tn = nbl;

  sprintf(ntitle,"%d Most Visited Broken Hyperlinks", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string4(fp,"Num","Hyperlink","Hits","Percentage",
			     "HTTP Code");

  for(i = 1; i<=tn ; i++)
  {
    PEDGE e = (PEDGE)randomized_select((GPTR *)varray,i-1,tlinks-1,1,
				       is_weight_lower,is_weight_bigger);
    
    if(e)
    {
      PNODE u = get_from_vertex(e);
      PNODE v = get_to_vertex(e);
      char * sanchor = get_label_edge(e);
      const char *att = v ? get_attribute(v,"code") : NULL;
      int http_code = att ? atoi(att) : -1;
      const char *utit = get_attribute(u,"title");
      const char *vtit = get_attribute(v,"title");
      char *ula = get_label_vertex(u);
      char *vla = get_label_vertex(v);
      char *scount = get_weight_edge(e);
      int cedge = scount ? atoi(scount) : 0;  
      float ave_nacc = ((float)cedge/(float)tacc)*100.0;
      char snum[20],save[20],scode[20];
      sprintf(snum,"%d", i);
      if(!vtit)
	  vtit = "No Info";
      if(!utit)
	  utit = "No Info";
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      sprintf(scode,"%d",http_code);
      write_row_num_hyperlink_string3(fp,snum,ula,utit,vla,vtit,sanchor,
				      scount,save,scode);
    }
  }
  write_end_table_html_file(fp);}

  if(tnlinks > 0) {
  if(nbl > tnlinks)
      tn = tnlinks;
  else
      tn = nbl;

  sprintf(ntitle,"%d Most Visited Independent Broken Hyperlinks", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string4(fp,"Num","Hyperlink","Hits","Percentage",
			     "HTTP Code");

  for(i = 1; i<=tn ; i++)
  {
    WebIndependentLinkPtr il = (WebIndependentLinkPtr)
	                        randomized_select((GPTR *)varray2,
				       i-1,tnlinks-1,1,
				       LOGR_is_access_il_lower,
				       LOGR_is_access_il_bigger);
    
    if(il)
    {
      const char *utit = LOGR_get_link_source_title(il);
      const char *vtit = LOGR_get_link_target_title(il);
      char *ula = (char *)LOGR_get_link_source_url(il);
      char *vla = (char *)LOGR_get_link_target_url(il);
      long nacc = LOGR_get_link_naccesses(il);
      float ave_nacc = ((float)nacc/(float)tacc)*100.0;
      int http_code = LOGR_get_link_target_http_code(il);
      char snum[20],scount[20],save[20],scode[20];      
      sprintf(snum,"%d", i);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      sprintf(scode,"%d",http_code);
      write_row_num_hyperlink_string3(fp,snum, ula,utit,vla,vtit,NULL,scount,
				      save,scode);
    }
  }
  write_end_table_html_file(fp); }

  if(tnurlbextref > 0) {
  if(nbl > tnurlbextref)
      tn = tnurlbextref;
  else
      tn = nbl;

  sprintf(ntitle,"%d Top External Broken Link URL Referers ", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string4(fp,"Num", "URL","Count","Percentage","HTTP Code");
  for(i = 1; i<=tn ; i++)
  {
    WebIndependentPagePtr opn = (WebIndependentPagePtr)randomized_select((GPTR *)varray3
					     ,i-1,tnurlbextref-1,1,
				LOGR_is_access_ip_lower,LOGR_is_access_ip_bigger);
    if(opn)
    {
      const char *url = LOGR_get_ip_url(opn);
      WebGenPagePtr gpn = (WebGenPagePtr)LOGR_get_ip_info(opn);
      const char *turl = gpn ? LOGR_get_gen_url(gpn) : NULL;
      const char *ttit = gpn ? LOGR_get_gen_title(gpn) : NULL;
      long nacc = LOGR_get_ip_naccesses(opn);
      float ave_nacc = ((float)nacc/(float)tacc)*100.0;
      int http_code = LOGR_get_ip_http_code(opn);
      char snum[20],scount[20],save[20],scode[20];
      sprintf(snum,"%d",i);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      sprintf(scode,"%d",http_code);
      /*char *tit = (char *)get_attribute(v,"title");*/
      write_row_num_hyperlink_string3(fp,snum,url,NULL,turl,ttit,NULL,
				      scount,save,scode);
    }
  }
  write_end_table_html_file(fp); }

  free(varray); varray = NULL;
  free(varray2); varray2 = NULL;
  free(varray3); varray3 = NULL;
}

void
write_body_top_blinks_html_file(GraphFrame *gf,FILE *fp,int nbl,
				WebStatPtr msn)
{
  LNode_type *lurl_ext_ref = LOGR_get_list_url_external_referers(msn);
  LNode_type *simple_links = LOGR_get_list_simple_links(msn);
  LNode_type *ptr = NULL;

  WebGenLinkPtr *varray = NULL;
  WebIndependentPagePtr *varray3 = NULL;

  int i=0;
  char ntitle[100];
  int tn=0;
  int tlinks = 0;
  int tnurlbextref = 0;
  double tsize = LOGR_get_total_site_size(msn);
  long tacc = LOGR_get_total_site_naccesses(msn);
  time_t now;
  char *scolor = "beige";

  for(ptr = simple_links->back; ptr != simple_links; ptr=ptr->back)
  {
    WebGenLinkPtr il = (WebGenLinkPtr)ptr->info;
    int code = LOGR_get_gen_http_code_target(il);
    if(code > 0 && code >=400)
	tlinks++;
  }

  varray = tlinks ? (WebGenLinkPtr *)calloc(tlinks,sizeof(WebGenLinkPtr)) : NULL;

  if(!varray && tlinks > 0) {
      fprintf(stderr,"Out of Memory write_body_top_blinks_html_file\n");
      abort();
  }

  if(tlinks) {
  i = 0;
  for(ptr = simple_links->back; ptr != simple_links; ptr=ptr->back)
  {
    WebGenLinkPtr il = (WebGenLinkPtr)ptr->info;
    int code = LOGR_get_gen_http_code_target(il);
    if(code > 0 && code >=400)
	varray[i++] = il;
  }
  }

  i=0;
  for(ptr = lurl_ext_ref->back; ptr != lurl_ext_ref; ptr = ptr->back)
  {
    WebIndependentPagePtr murle = (WebIndependentPagePtr)ptr->info;
    if(LOGR_get_ip_http_code(murle) >= 400)
	i++;
  }
  tnurlbextref = i;

  varray3 = tnurlbextref ? (WebIndependentPagePtr *)calloc(tnurlbextref,sizeof(WebIndependentPagePtr)) : NULL;

  if(!varray3 && tnurlbextref > 0) {
      fprintf(stderr,"Out of Memory write_body_top_ref_html_file\n");
      abort();
  }

  if(tnurlbextref) {
  i=0;
  for(ptr = lurl_ext_ref->back; ptr != lurl_ext_ref; ptr = ptr->back)
  {
    WebIndependentPagePtr murle = (WebIndependentPagePtr)ptr->info;
    if(LOGR_get_ip_http_code(murle) >= 400)
	varray3[i++] = murle;
  }
  }


  now = time(NULL);
  write_header_html_file(fp,1,"List of Top Broken Links");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");  
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Unique Broken Links : ");
  fprintf(fp, " %d URLs\n",tlinks);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Unique External Broken Links : ");
  fprintf(fp, " %d URLs\n",tnurlbextref);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Access Count : ");
  fprintf(fp, " %ld \n",tacc);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Size : ");
  fprintf(fp, " %.2f K Bytes\n",(float)(tsize/1024.0));
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RMVBLString);
  write_horizontal_rule_html_file(fp);

  if(tlinks > 0){
  if(nbl > tlinks)
      tn = tlinks;
  else
      tn = nbl;

  sprintf(ntitle,"%d Most Visited Broken Hyperlinks", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string4(fp,"Num","Hyperlink","Hits","Percentage",
			     "HTTP Code");

  for(i = 1; i<=tn ; i++)
  {
    WebGenLinkPtr il = (WebGenLinkPtr)randomized_select((GPTR *)varray,i-1,
					tlinks-1,1,
					LOGR_is_access_gen_link_lower,
					LOGR_is_access_gen_link_bigger);
    
    if(il)
    {
      const char *utit = LOGR_get_gen_title_source(il);
      const char *vtit = LOGR_get_gen_title_target(il);
      const char *sanchor = LOGR_get_gen_link_label(il);
      char *ula = (char *)LOGR_get_gen_url_source(il);
      char *vla = (char *)LOGR_get_gen_url_target(il);
      long nacc = LOGR_get_gen_link_naccesses(il);
      float ave_nacc = ((float)nacc/(float)tacc)*100.0;
      int http_code = LOGR_get_gen_http_code_target(il);
      char snum[20],scount[20],save[20],scode[20];      
      sprintf(snum,"%d", i);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      sprintf(scode,"%d",http_code);
      if(LOGR_is_gen_link_independent(il))
	  write_row_num_hyperlink_string3_color(fp,snum, ula,utit,vla,vtit,
					   sanchor,scount,save,scode,scolor);
      else
	  write_row_num_hyperlink_string3_color(fp,snum, ula,utit,vla,vtit,
					   sanchor,scount,save,scode,NULL);
    }
  }
  write_end_table_html_file(fp);}


  if(tnurlbextref > 0) {
  if(nbl > tnurlbextref)
      tn = tnurlbextref;
  else
      tn = nbl;

  sprintf(ntitle,"%d Top External Broken Link URL Referers ", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string4(fp,"Num", "URL","Count","Percentage","HTTP Code");
  for(i = 1; i<=tn ; i++)
  {
    WebIndependentPagePtr opn = (WebIndependentPagePtr)randomized_select((GPTR *)varray3
					     ,i-1,tnurlbextref-1,1,
				LOGR_is_access_ip_lower,LOGR_is_access_ip_bigger);
    if(opn)
    {
      const char *url = LOGR_get_ip_url(opn);
      WebGenPagePtr gpn = (WebGenPagePtr)LOGR_get_ip_info(opn);
      const char *turl = gpn ? LOGR_get_gen_url(gpn) : NULL;
      const char *ttit = gpn ? LOGR_get_gen_title(gpn) : NULL;
      long nacc = LOGR_get_ip_naccesses(opn);
      float ave_nacc = ((float)nacc/(float)tacc)*100.0;
      int http_code = LOGR_get_ip_http_code(opn);
      char snum[20],scount[20],save[20],scode[20];
      sprintf(snum,"%d",i);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      sprintf(scode,"%d",http_code);
      /*char *tit = (char *)get_attribute(v,"title");*/
      write_row_num_hyperlink_string3(fp,snum,url,NULL,turl,ttit,NULL,
				      scount,save,scode);
    }
  }
  write_end_table_html_file(fp); }

  free(varray); varray = NULL;
  free(varray3); varray3 = NULL;
}



